package com.gxa.dao.impl;

import com.gxa.dao.UserDao;
import com.gxa.entity.User;
import com.gxa.utils.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public User getByUsernameAndPwd(String username, String pwd) throws SQLException {
        //获取连接
        Connection connection = DBConnection.getConnection();
        //准备SQL语句
        String sql ="SELECT * FROM user WHERE username=? AND pwd=?";

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //执行SQL语句
            ps = connection.prepareStatement(sql);
            //设置参数
            ps.setString(1,username);
            ps.setString(2,pwd);

            rs = ps.executeQuery();

            if(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(username);
                user.setPwd(pwd);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBConnection.close(rs,ps,connection);
        }

        return null;
    }
}
